package com.zzyl.exception;

import com.zzyl.base.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author YiSheep
 * @date 2024/08/06
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理程序基异常
     *
     * @param e 异常对象
     * @return {@code ResponseResult<String> }
     * @author YiSheep
     */
    @ExceptionHandler(BaseException.class)
    public ResponseResult<String> handlerBaseException(BaseException e) {
        //控制台打印错误信息
        e.printStackTrace();
        log.error(e.getBasicEnum().getMsg());
        return ResponseResult.error(e.getBasicEnum());
    }


    /**
     * 处理程序运行时异常
     *
     * @param e 异常对象
     * @return {@code ResponseResult<String> }
     * @author YiSheep
     */
    @ExceptionHandler(RuntimeException.class)
    public ResponseResult<String> handlerRuntimeException(RuntimeException e) {
        //控制台打印错误信息
        e.printStackTrace();
        return ResponseResult.error(e.getMessage());
    }


    /**
     * 处理程序未知异常
     *
     * @param e 异常对象
     * @return {@code ResponseResult<String> }
     * @author YiSheep
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult<String> handlerUnKnowException(Exception e) {
        //控制台打印错误信息
        e.printStackTrace();
        return ResponseResult.error(e.getMessage());
    }

}
